/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG                        *
*Copyright (C) 2006, Tony Sparks                                                     *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "LUA_EntityView.h"
#include "../render/viewmanager.h"
#include "../game/entitymanager.h"
#include "../game/ientity.h"
#include "../render/image.h"
#include "../kernel.h"

extern Kernel* g_kernel;

LUA_EntityView::LUA_EntityView(void)
{
}

LUA_EntityView::~LUA_EntityView(void)
{
}

EntityView* LUA_EntityView::m_view = NULL;

/** Create the model */
int LUA_EntityView::Create(lua_State *L)
{
    ViewManager* vMng = ViewManager::GetInstance();
    // retrieve the instance of the entitymanager
    EntityManager* entMng = EntityManager::GetInstance();
    // get the name of the entity
    std::string name = luaL_checkstring( L, 1 );

    // find the entity associated with this model
    IEntity* ent = entMng->Find( name );
    if ( !ent )
    {
        // TODO - Print error
        return 0;
    }
    // create the view
    m_view = vMng->CreateView( ent );
    m_view->SetVisible( true );

	return 0;
}


/** Attach to an entity */
int LUA_EntityView::AttachTo( lua_State *L )
{

    return 0;
}
/** Set the image */
int LUA_EntityView::SetImage( lua_State *L )
{
        // get the name of the image
    std::string name = luaL_checkstring( L, 1 );
    int row = (int)luaL_checknumber( L, 2 );
    int col = (int)luaL_checknumber( L, 3 );

    Image image = g_kernel->GetResource()->LoadImages( name, row, col ); 
    if ( m_view )
        m_view->GetModel()->SetImage( image );
	return 0;
}

/** Set state to tiles */
int LUA_EntityView::SetTiles( lua_State *L )
{
    // the new state
    std::string state = luaL_checkstring( L, 1 );
    
    // param lists
    int f, r, a,c;
    f = 2; r = 3; c = 4;a = 5;

    // create our model directions
    ModelDirections* dirs = new ModelDirections;
    // now retrieve info for the four directions
    for ( int i = 2; i < 6; i++ )
    {
        int fps = (int)luaL_checknumber( L, f );
        int row = (int)luaL_checknumber( L, r );
        int col = (int)luaL_checknumber( L, c );
        int amt = (int)luaL_checknumber( L, a );
        // update the parameter lists
        f += 4; r += 4; a += 4;

        // create the animation
        //Animation* anim = new Animation(fps, row, amt);
        // directions go from north then clockwise
        dirs->AddDirection( i-2, fps, row, col, amt );
    }
    if ( m_view )
        // add the state to the model
        m_view->GetModel()->AddState( state, dirs );
    else // if no model, we cannot have this wasting memory
        delete dirs;
	return 0;
}

// -- Bind to LUA
#define method(name) {#name, LUA_EntityView::name}

const luaL_reg LUA_EntityView::methods[] = {
        method(Create),
        method(AttachTo),
        method(SetImage),
        method(SetTiles),
        {NULL, NULL}
};

// - Bind to lua
int luaopen_LUA_EntityView(lua_State *L) {
    luaL_openlib(L, "entityview", LUA_EntityView::methods, 0);
 	return 0;
}
